<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-180px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="msgPhone">
        <el-input
          v-model="queryParams.msgPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-180px"
        />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="msgEmail">
        <el-input
          v-model="queryParams.msgEmail"
          placeholder="请输入电子邮箱"
          clearable
          @keyup.enter="handleQuery"
          class="!w-180px"
        />
      </el-form-item>
      <el-form-item label="类型" prop="guestbookType">
        <el-select
          v-model="queryParams.guestbookType"
          placeholder="请选择类型"
          clearable
          class="!w-120px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_GUESTBOOK_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable class="!w-120px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_GUESTBOOK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公开" prop="isOpen">
        <el-select v-model="queryParams.isOpen" placeholder="请选择" clearable class="!w-120px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_OPEN_CLOSE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="回复" prop="isReplied">
        <el-select v-model="queryParams.isReplied" placeholder="请选择" clearable class="!w-120px">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_OPEN_CLOSE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推荐" prop="recommended">
        <el-select
          v-model="queryParams.recommended"
          placeholder="请选择"
          clearable
          class="!w-120px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_OPEN_CLOSE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cms:guestbook:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="danger" @click="handleBatchDelete()" v-hasPermi="['cms:guestbook:delete']">
          删除
        </el-button>
        <el-select
          style="margin-left: 10px"
          class="!w-120px"
          v-model="changeStatusParam.status"
          placeholder="状态修改为"
          @change="handleBatchChangeStatus"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMS_GUESTBOOK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-select
          style="margin-left: 10px"
          class="!w-120px"
          v-model="changeStatusParam.isOpen"
          placeholder="公开"
          @change="handleBatchChangeOpen"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMS_OPEN_CLOSE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-select
          style="margin-left: 10px"
          class="!w-120px"
          v-model="changeStatusParam.recommended"
          placeholder="推荐"
          @change="handleBatchChangeRecommend"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMS_OPEN_CLOSE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      ref="tableRef"
      row-key="id"
      @selection-change="handleDataSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        width="400px"
        show-overflow-tooltip
      />
      <el-table-column label="类型" align="center" prop="guestbookType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_GUESTBOOK_TYPE" :value="scope.row.guestbookType" />
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contactUser" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_GUESTBOOK_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="公开" align="center" prop="isOpen">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_OPEN_CLOSE" :value="scope.row.isOpen" />
        </template>
      </el-table-column>
      <el-table-column label="回复" align="center" prop="isReplied">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_OPEN_CLOSE" :value="scope.row.isReplied" />
        </template>
      </el-table-column>
      <el-table-column label="推荐" align="center" prop="recommended">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_OPEN_CLOSE" :value="scope.row.recommended" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms:guestbook:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:guestbook:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <GuestbookForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts" name="Guestbook">
// 全局相关的 import
import { ref } from 'vue'
import GuestbookForm from './GuestbookForm.vue'
import { dateFormatter } from '@/utils/formatTime'
import { getStrDictOptions, getIntDictOptions } from '@/utils/dict'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import * as GuestbookApi from '@/api/cms/guestbook'
import { DICT_TYPE } from '@/utils/dict'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  title: '',
  guestbookType: undefined,
  msgPhone: '',
  msgEmail: '',
  isOpen: undefined,
  isReplied: undefined,
  recommended: undefined,
  status: undefined,
  msgIp: ''
})
const tableRef = ref() // 表格的 Ref
const ids = ref<number[]>([]) // 表格的选中 ID 数组
/** 当主操作表格选择项发生变化时会触发该事件  */
const handleDataSelectionChange = (array: GuestbookApi.GuestbookVO[]) => {
  ids.value = []
  if (!array) {
    return
  }
  array.forEach((row) => ids.value.push(row.id))
}
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await GuestbookApi.getGuestbookPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await GuestbookApi.deleteGuestbook(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const changeStatusParam = reactive({
  ids: [],
  status: undefined,
  isOpen: undefined,
  recommended: undefined
})
//批量修改状态
const handleBatchChangeStatus = async () => {
  if (ids.value.length === 0) {
    return
  }
  if (changeStatusParam.status == -1) {
    return
  }
  changeStatusParam.ids = []
  changeStatusParam.ids.push(...ids.value)
  try {
    // 二次确认
    await message.confirm('确认修改状态吗')
    // 发起
    await GuestbookApi.updateGuestbookStatus(changeStatusParam)
    message.success(t('common.success'))
    tableRef.value.clearSelection()
    // 刷新列表
    await getList()
  } catch {}
}
//批量修改公开
const handleBatchChangeOpen = async () => {
  if (ids.value.length === 0) {
    return
  }
  changeStatusParam.ids = []
  changeStatusParam.ids.push(...ids.value)
  try {
    // 二次确认
    await message.confirm('确认操作吗')
    // 发起
    await GuestbookApi.updateGuestbookOpen(changeStatusParam)
    message.success(t('common.success'))
    tableRef.value.clearSelection()
    // 刷新列表
    await getList()
  } catch {}
}
//批量修改推荐
const handleBatchChangeRecommend = async () => {
  if (ids.value.length === 0) {
    return
  }
  changeStatusParam.ids = []
  changeStatusParam.ids.push(...ids.value)
  try {
    // 二次确认
    await message.confirm('确认操作吗')
    // 发起
    await GuestbookApi.updateGuestRecommend(changeStatusParam)
    message.success(t('common.success'))
    tableRef.value.clearSelection()
    // 刷新列表
    await getList()
  } catch {}
}
//批量删除
const handleBatchDelete = async () => {
  let guestbookIds = ids.value
  if (guestbookIds.length === 0) {
    return
  }
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await GuestbookApi.deleteGuestbooks(guestbookIds)
    message.success(t('common.delSuccess'))
    tableRef.value.clearSelection()
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
